'''
Created on Feb 20, 2011
Parse the quarterly metrics tebles.
@author: solagna
'''

import ParseCommon
import WikiDownloader.FileManager
fm = WikiDownloader.FileManager.FileManager()

__HUMAN_TABLE          = 1
__OPERATIONS_TABLE     = 2
__INFRASTRUCTURE_TABLE = 3
__OTHER_TABLE          = 4
#The first two columns don't want comments.
__comments = ("","","<!--Staged rollout components -->","<!--  Num. patches staged rollout -->","<!-- Num. MPI sites -->", "<!--  Num. MW Stacks deployed -->", "<!--  Num. HPC -->", "<!--  Virtualizaed Resources (HEP SPEC06) -->", "<!--  Sites in production / Sites pledged -->", "<!--  Logical CPUs in production / Logical CPUs pledged -->", "<!--  Disk capacity in production / Disk capacity pledged -->", "<!--  Opened tickets -->", "<!--  Avg ticket solution time/Median Solution time-->", "<!--  Average ticket response time -->", "<!--  Num. Top BDII -->", "<!--Num. WMS -->","<!-- Availability -->","<!--Suspended sites-->","<!--is it a new operations centre-->")
 
def addComments (table):
    '''
    Add comments specific for the MS109 table.
    ''' 
    for x in range(len(table)):
        for y in range(len(table[x])):
            table[x][y] += __comments[y]
def getMS109Header(table_num):
    '''
    Returns a string with the header in the Xth table of the MS109 assessment rate. Use Albania as default partner.
    '''
    partner_file = fm.getMS109File("Albania")
    soup = ParseCommon.soupify(partner_file)         
    table = ParseCommon.findTableX(soup, table_num)
    
    header_first = ParseCommon.parseTableColHeader(table,1)#ParseCommon.parseTableYColHeader(table)
    header_last  = ParseCommon.parseTableColHeader(table,3)
    
    headers_zip = zip (header_first, header_last)
    headers     = [' '.join(i) for i in headers_zip]

    return headers
    
def getMS109Headers ():
    '''
    Returns a string with all the headers in the MS109 assessment rate. Use Albania as default partner.
    '''
    partner_file = fm.getMetrFile("Albania")
    soup = ParseCommon.soupify(partner_file)
    
def getMS109PartnerValues(Partner):
    partner_file = fm.getMS109File(Partner)
    print "File: "+partner_file
    soup = ParseCommon.soupify(partner_file)
    #Get the 4th column from all the four tables.
    rates_column = []
    rates_column.extend(ParseCommon.parseColumnYfromTableX (soup, 3, __HUMAN_TABLE, True))
    rates_column.extend(ParseCommon.parseColumnYfromTableX (soup, 3, __OPERATIONS_TABLE, True))
    rates_column.extend(ParseCommon.parseColumnYfromTableX (soup, 3, __INFRASTRUCTURE_TABLE, True))
    rates_column.extend(ParseCommon.parseColumnYfromTableX (soup, 3, __OTHER_TABLE, True))
    
    return rates_column

def getAllMS109Values():
    '''
    Returns a table with all the auto-rates 
    '''
    endTable = []
    partners = fm.getMS109Partner()
    for part in partners:
        tmpList = getMS109PartnerValues(part)
        tmpList.insert(0, part) #Add the partner name as first element of the row
        endTable.append(tmpList)
    return endTable

if __name__ == "__main__":
    metric = getMS109Header(1)
    
    for row in metric:
        print row